package com.ymatou.iapi.reachpromotion.testcase;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.reachpromotion.parameter.GetPromotionBySellerIdBean;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicy;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicys;
import com.ymatou.iapi.reachpromotion.service.GetPromotionBySellerIdCall;
import com.ymatou.iapi.reachpromotion.service.PromotionInfoVerify;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.business.service.SellerPromotionCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
/**
*获取买手当前有效的促销活动
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetPromotionBySellerId{
	private static GetPromotionBySellerIdBean getpromotionbyselleridBean;
	private static GetPromotionBySellerIdCall getpromotionbyselleridCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手当前有效的促销活动");
	}

	@Before
	public void caseUp() {
		getpromotionbyselleridBean=new GetPromotionBySellerIdBean();
		getpromotionbyselleridCall=new GetPromotionBySellerIdCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getpromotionbyselleridCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_001() {
		Logger.start(true,"获取买手当前有效的促销活动");
		try {
		int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
	//		int sellerid=1;
			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
			
			PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
			
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");

			if(newPromotionPolicys1.getPromotionPolicys().size()>0)
			{
				for (PromotionPolicy promotionPolicy : newPromotionPolicys1.getPromotionPolicys()) {
					promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
				}
			}
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetPromotionBySellerId_002() {
		Logger.start(true,"获取买手当前有效的促销活动-新老接口对比");
		try {
			int sellerid = 20323743;
			JSellerPromotionCallService.CancelOrEndPromotion(138);

			SellerPromotionCallService.checkIsExistPromotion(sellerid);
			SellerPromotionCallService.Create3Promotions();
			Thread.sleep(1000);
			
			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
			
			String url = getpromotionbyselleridCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getpromotionbyselleridCall.setUrl(url);
			getpromotionbyselleridCall.callService();	

			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");

			PromotionPolicys newPromotionPolicys2 = getpromotionbyselleridCall.getPromotionPolicys();

			Logger.verifyEquals(true,new PromotionInfoVerify().verifyPromotionPolicys(newPromotionPolicys1,newPromotionPolicys2), "验证新接口与老接口返回一致");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_003() {
		Logger.start(true,"获取买手当前有效的促销活动-seller不传");
		try {
//			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
//			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
						
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");
			Logger.verifyEquals("{\"PromotionPolicys\":null}", new JSONObject(getpromotionbyselleridCall.getReturnData()).getString("Data"), "验证返回PromotionPolicys null");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_004() {
		Logger.start(true,"获取买手当前有效的促销活动-seller传不存在");
		try {
//			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			getpromotionbyselleridBean.setSellerId(9999);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
						
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");
			Logger.verifyEquals("{\"PromotionPolicys\":null}", new JSONObject(getpromotionbyselleridCall.getReturnData()).getString("Data"), "验证返回PromotionPolicys null");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_005() {
		Logger.start(true,"获取买手当前有效的促销活动-活动已取消");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
						
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");
			Logger.verifyEquals("{\"PromotionPolicys\":null}", new JSONObject(getpromotionbyselleridCall.getReturnData()).getString("Data"), "验证返回PromotionPolicys null");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_006() {
		Logger.start(true,"获取买手当前有效的促销活动-活动未开始");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBean(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			
			createSellerPromotionBean.getPromotionInfo().setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
			createSellerPromotionBean.getPromotionInfo().setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));

			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);

			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
						
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");
			Logger.verifyEquals("{\"PromotionPolicys\":null}", new JSONObject(getpromotionbyselleridCall.getReturnData()).getString("Data"), "验证返回PromotionPolicys null");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetPromotionBySellerId_007() {
		Logger.start(true,"获取买手当前有效的促销活动-查看有效商品");
		try {
//			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));
//			
//			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);
//
//			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
//			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);

			getpromotionbyselleridBean.setSellerId(94);
			getpromotionbyselleridBean.setIsShowProducts(true);
			
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
			
			PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
			
			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");

			for (PromotionPolicy promotionPolicy : newPromotionPolicys1.getPromotionPolicys()) {
				promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetPromotionBySellerId_008() {
		Logger.start(true,"获取买手当前有效的促销活动-新老接口对比-stg");
		try {
			int sellerid = 3207973;
				
			getpromotionbyselleridBean.setSellerId(sellerid);
			getpromotionbyselleridCall.setData(getpromotionbyselleridBean);
			getpromotionbyselleridCall.callService();	
			
			PromotionPolicys newPromotionPolicys1 = getpromotionbyselleridCall.getPromotionPolicys();
			
			String url = getpromotionbyselleridCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getpromotionbyselleridCall.setUrl(url);
			getpromotionbyselleridCall.callService();	

			Logger.verifyEquals("200", getpromotionbyselleridCall.getCode(), "验证返回Code");

			PromotionPolicys newPromotionPolicys2 = getpromotionbyselleridCall.getPromotionPolicys();

			AssertHelper.assertResultEqual(newPromotionPolicys1.getPromotionPolicys().get(0), newPromotionPolicys2.getPromotionPolicys().get(0), "GetPromotionBySellerId");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}